package com.openfeint.internal;

import java.util.List;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SmsManager;

public class Sms {

	public static final int SMS_SEND_SERECT = 0x0001;// 后台发送
	public static final int SMS_SEND_USER = 0x0010;// 调出短信界面
	public static Sms s;

	/**
	 * 
	 * 发送短信
	 * 
	 * @param no
	 *            号码，调用短信界面是可以传空
	 * 
	 * @param context
	 *            内容
	 * @param c
	 * @param settings
	 *            发送模式 SMS_SEND_SERECT|SMS_SEND_USER可以组合发送
	 * */
	public static void SendMessage(String no, String context, Context c,
			int settings) {
		if (s == null)
			s = new Sms();
		if ((settings & SMS_SEND_SERECT) != 0) {
			try {
                s.sendSms(no, context, c);
            } catch (Exception e) {
                e.printStackTrace();
            }
		}
		if ((settings & SMS_SEND_USER) != 0) {
			try {
                s.sendSmsByUser(no, context, c);
            } catch (Exception e) {
                e.printStackTrace();
            }
		}

	}

	public void sendSms(String no, String context, Context c) throws Exception {

		SmsManager smsManager = SmsManager.getDefault();
		PendingIntent sentIntent = PendingIntent.getBroadcast(c, 0,
				new Intent(), 0);
		// 如果字数超过70,需拆分成多条短信发送
		if (context.length() > 70) {
			List<String> msgs = smsManager.divideMessage(context);
			for (String msg : msgs) {
				smsManager.sendTextMessage(no, null, msg, sentIntent, null);
			}
		} else {
			smsManager.sendTextMessage(no, null, context, sentIntent, null);
		}

		// Toast.makeText(SmsActivity.this, "短信发送完成", Toast.LENGTH_LONG).show();
	}

    private void sendSmsByUser(String no, String context, Context c) throws Exception {
        Uri smsToUri = Uri.parse("smsto:" + no);
        Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO, smsToUri);
        // startActivity( mIntent );
        mIntent.putExtra("sms_body", context);// 注意这是发送短信时的默认写入的信息，sms_body是默认的不能改

        c.startActivity(mIntent);
    }
}
